package fmanganiello.tests.javatests.sort;

public class Sorter {
	private static <T extends Number> void internalQuickSort(T[] array, int start, int end) {
		if (start < end) {
			int pivot = start;
			int left = start;
			int right = end;
			T pivotVal = array[pivot];
			
			while (left < right) {
				for (; left < array.length && left <= end && array[left].doubleValue() <= pivotVal.doubleValue(); ++left);
				for (; right >= 0 && array[right].doubleValue() > pivotVal.doubleValue(); --right);				
				if (left < right) {
					T tmp = array[left];
					array[left] = array[right];
					array[right] = tmp;
				}
			}
			
			array[start] = array[right];
			array[right] = pivotVal;
			internalQuickSort(array, start, right-1);
			internalQuickSort(array, right+1, end);
		}
	}
	
	public static <T extends Number> void quickSort(T[] array) {
		internalQuickSort(array, 0, array.length-1);
	}
}
